package pers.w.chaptertalk.funs;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.File;
import java.util.HashMap;

import pers.w.chaptertalk.R;

public class BookInfo {
    private static final File contentDir = new File(Fun.rootPath, "content");

    // public String name, uri, author, introduction, img, type, lastChapterName, lastUpdateTime;
    protected HashMap<Integer, String> bookifoMap;
    public static final File imageDir = new File(Fun.rootPath, "cover");

    public File getBookDir() {
        return bookDir;
    }

    private File bookDir ;
    public Long getID() {
        return id;
    }

     public void setID(Long id) {
        this.id = id;
       mkdir();
    }

    public void mkdir(){
        if (id>0){
        bookDir  =new File(contentDir,"B"+ id);
        if (!bookDir.exists())
            bookDir.mkdir();}
    }
    public void mkdir(File dir){
        bookDir  =dir;
        if (!bookDir.exists())
            bookDir.mkdir();
    }
    private long id=-1;

    public Bitmap getImage_bmp() {
        return image_bmp;
    }

    public void setImage_bmp(Bitmap image_bmp) {
        this.image_bmp = image_bmp;
    }

    private Bitmap image_bmp;


    public static int[] BookInfoFields = {
            R.string.info_name, R.string.info_uri, R.string.info_author, R.string.info_introduction,
            R.string.info_img, R.string.info_type, R.string.info_lastChapterName, R.string.info_lastUpdateTime};

    public BookInfo() {
        if (!BookInfo.contentDir.exists())
            BookInfo.contentDir.mkdir();
        bookifoMap = new HashMap<>();
        for (int field : BookInfoFields) {
            bookifoMap.put(field, "");
        }
        if (!imageDir.exists()) {
            imageDir.mkdirs();
        }
    }

    public BookInfo(HashMap<Integer, String> bookifoMap) {
        if (!BookInfo.contentDir.exists())
            BookInfo.contentDir.mkdir();
        this.bookifoMap = bookifoMap;
        if (!imageDir.exists()) {
            imageDir.mkdirs();
        }
    }

    public void setFieldValue(int field, String data) {
        bookifoMap.put(field, data);
    }


    private static String[] fields =null;

    public static String[] getFieldNames(Resources R) {

        if (null == fields) {
            fields =new String[BookInfoFields.length];
            for (int i = 0; i < fields.length; i++) {
                fields[i] = R.getString(BookInfoFields[i]);
            }
        }
        return fields;
    }

    public String getFieldValue(Integer field) {
        return bookifoMap.get(field);
    }

    public HashMap<Integer, String> getBookifoMap() {
        return bookifoMap;
    }
    public void delBookDir(){
        new Thread(() -> {
            if (null!=bookDir && bookDir.exists()){
                String[] files = bookDir.list();
                if (null!=files){
                for (String f :files){
                    File file = new File(bookDir,f);
                    file.delete();
                }}
                bookDir.delete();
                setID(0L);
            }

        }).start();

    }

    /*public String buildImgName() {
        String str = bookifoMap.get(R.string.info_img);
        assert str != null;
        if (!str.isEmpty()) {
            StringBuilder sb_name = new StringBuilder();

            for (int i = str.indexOf("/",8); i > 0 && i < str.length(); i++) {
                sb_name.append((int)str.charAt(i));
            }
            return sb_name.toString();// str.substring(8).replace("/", "").replace(".", "");
        }
        return "";
    }*/

    public boolean saveBmp() {
        if (null != image_bmp) {
            String img_name = id +".jpg"; //buildImgName();
            if (!img_name.isEmpty()) {
                final File imgPath = new File(imageDir, img_name);
                if (!imgPath.exists())
                return Fun.writeBitmap(imgPath, image_bmp);
            }
        }
        return false;
    }

    public void readBmp() {
        final File imgPath = new File(imageDir, id +".jpg"/*buildImgName()*/);
        if (imgPath.exists()) {
            image_bmp = BitmapFactory.decodeFile(imgPath.getPath());
        }
    }

    public void readBmp(String imgPath) {
        image_bmp = BitmapFactory.decodeFile(imgPath);
    }
    public void delBmp(){
        final File imgPath = new File(imageDir, id +".jpg"/*buildImgName()*/);
        if (imgPath.exists()) {
            imgPath.delete();
        }
    }
}